<!DOCTYPE html>
<html>
<head>
    <title>收费项目登记</title>
    <meta charset="UTF-8">
    <link rel="stylesheet" type="text/css" href="../Css/bootstrap.css" />
    <link rel="stylesheet" type="text/css" href="../Css/bootstrap-responsive.css" />
    <link rel="stylesheet" type="text/css" href="../Css/style.css" />
    <script type="text/javascript" src="../Js/jquery.js"></script>
    <script type="text/javascript" src="../Js/bootstrap.js"></script>
    <script type="text/javascript" src="../Js/ckform.js"></script>
    <script type="text/javascript" src="../Js/common.js"></script>

    <style type="text/css">
        body {
            padding-bottom: 40px;
        }
        .sidebar-nav {
            padding: 9px 0;
        }

        @media (max-width: 980px) {
            /* Enable use of floated navbar text */
            .navbar-text.pull-right {
                float: none;
                padding-left: 5px;
                padding-right: 5px;
            }
        }


    </style>
    <script type="text/javascript">
    	function checkall(){
			var alls=document.getElementsByName("check");
			var ch=document.getElementById("checkall");
			if(ch.checked){
				for(var i=0;i<alls.length;i++){
					alls[i].checked=true;	
				}	
			}else{
				for(var i=0;i<alls.length;i++){
					alls[i].checked=false;	
				}	
			}
		}
		function delAll(){
			var alls=document.getElementsByName("check");
			var ids=new Array();
			for(var i=0;i<alls.length;i++){
				if(alls[i].checked){
					ids.push(alls[i].value);
				}		
			}
			if(ids.length>0){
				if(confirm("确认操作?")){
					alert("成功!");
				}
			}else{
				alert("请选中要操作的项");
			}
		}
    </script>
</head>
<body>
   
<div class="definewidth m20">
<table class="table table-bordered table-hover definewidth m10">
    <tr>
        <td width="10%" class="tableleft">收费项目名称：</td>
        <td width="10%"><input type="text" id="pname"  value=""/></td>
		
        <td>
		<button type="button" class="btn btn-primary" onclick="queryList(1)">查询</button>
			<button type="reset" class="btn btn-primary" id="res">清空</button> </td>
    </tr>
   
</table>


</div>
<table class="table table-bordered table-hover definewidth m10" >
   <thead>
    <tr>
        <th>编号</th>
        <th>收费项目名称</th>        
        <th>收费金额</th>
        <th>创建日期</th>
        <th>操作</th>
    </tr>
    </thead>
    <tbody id="tbody">
    </tbody>
  </table>
  
  <table class="table table-bordered table-hover definewidth m10" >
  	<tr>
        <th colspan="5">
<!--            <div class="inline pull-right page" id = "pageInfo">-->
<!--          <a href='#' >第一页</a> <a href='#'>上一页</a>-->
<!--          <span class='current'>1</span><a href='#'>2</a>-->
<!--          <a href='/chinapost/index.php?m=Label&a=index&p=3'>3</a>-->
<!--          <a href='#'>4</a>-->
<!--          <a href='#'>5</a>-->
<!--          <a href='#' >下一页</a>-->
<!--          <a href='#' >最后一页</a>-->
<!--		  &nbsp;&nbsp;&nbsp;共<span class='current' id="total"></span>条记录<span class='current' id="pages"> </span>页-->
<!--		  </div>-->
            <div id="pageInfo-box" class="inline pull-right page">
            </div>
		 <div><button type="button" class="btn btn-success" id="newNav">添加收费项目</button>
		 
		 </div>
		 
		 </th>
    </tr>
  </table>
  
</body>
</html>
<script type="text/javascript">
    layer.load(0);
    // TODO: 这些在common.js定义过了
    // String.prototype.GetValue= function(para) {
    //     let reg = new RegExp("(^|&)"+ para +"=([^&]*)(&|$)");
    //     let r = this.substr(this.indexOf("\?")+1).match(reg);
    //     if (r!=null) return unescape(r[2]); return null;
    // }
    // // 地址参数
    // let globalPageNum = document.location.toString().GetValue("pageNum");
    // let globalPageSize = document.location.toString().GetValue("pageSize");
    // // 优先url地址栏参数
    // if(globalPageNum == undefined || globalPageNum == '' || globalPageNum == 'null'){
    //     // url读取不到从input获取
    //     globalPageNum = $(".current").text();
    //     if(globalPageNum == undefined || globalPageNum == '' || globalPageNum == 'null'){
    //         // input没有赋初值
    //         globalPageNum = 1;
    //     }
    // }
    // // 优先url地址栏参数
    // if(globalPageSize == undefined || globalPageSize == '' || globalPageNum == 'null'){
    //     // url读取不到从input获取
    //     globalPageSize = $("#pageSize").val();
    //     if(globalPageSize == undefined || globalPageSize == '' || globalPageSize == 'null'){
    //         // input没有赋初值
    //         globalPageSize = 5;
    //     }
    // }

    $(function () {
        $('#newNav').click(function(){
            window.location.href="charge-add.html?pageNum=" + globalPageNum + "&pageSize=" + globalPageSize;
        });
        $('#backid').click(function(){
            window.location.href="dispensing.html?pageNum=" + globalPageNum + "&pageSize=" + globalPageSize;
        });
        // 分页查询
        queryList(globalPageNum);
        // 监听清空
        res();
    });

    // 清空
    function res(){
        $("#res").on("click", function(){
            $("#pname").val("");
            queryList(1);
        })
    }

    queryList = function(pageNum){
        let chapName = $("#pname").val();
        layer.load(0);
        $.ajax({
            type: "GET",
            url: origin + "/charge/list/?pageNum="+pageNum+"&pageSize="+globalPageSize,
            data:{chapName:chapName,jwtToken:jwtToken},
            xhrFields: {
                withCredentials: true
            },
            crossDomain: true,
            success: function (data) {
                layer.closeAll();
                console.log(data);
                if(data.code == 200){
                    // 填充分页
                    fillPageData(data.obj);
                    // 更新当前页全局变量
                    globalPageNum = $(".current").text();
                    // 更新每页条数全局变量
                    globalPageSize = $("#pageSize").val();
                    // 填充内容
                    loadDate(data);
                }else{
                }
            }
            // TODO: 后端拦截ajax请求进行登陆跳转需要前端相互配合, 每一个ajax请求都需要加上以下的代码
            ,complete : function(xhr, status) {
                myComplete(xhr, status);
            }
        });
    }
    function loadDate(data) {
        let list = data.obj.list;

        let str = "";
        for (let i =0;i<list.length;i++){
            let obj = list[i];
            let id=obj.chapId==null?'':obj.chapId;
            let name=obj.chapName==null?'':obj.chapName;
            let money=obj.chapMoney==null?'':obj.chapMoney;
            let createtime=obj.chapCreateTime==null?'':obj.chapCreateTime;
            str+='<tr>' +
                '            <td>'+id+'</td>' +
                '            <td>'+name+'</td>' +
                '            <td>'+money+'</td>' +
                '            <td>'+createtime+'</td>' +
                '            <td style="vertical-align:middle;">'+
                                '<a href="#" id="update" onclick="updateCharge('+id+')">'+"修改&nbsp;&nbsp;"+'</a>'+
                                '<a href="#" id="delete" onclick="deleteCharge('+id+')">'+"删除&nbsp;&nbsp;"+'</a>'+
                                '<a href="#" id="chick" onclick="chickCharge('+id+')">'+"查看详情"+'</a>'+
                '            </td>'
                '</tr>'

        }
        $("#tbody").html($(str));

   }
    function updateCharge(id) {
        // window.location.href="/hospital/charge-edit1.html?chapId="+id;
        window.location.href="charge-edit1.html?chapId="+id +
            "&globalPageNum" + globalPageNum + "&globalPageSize" + globalPageSize;
    }
    function chickCharge(id) {
        // window.location.href="/hospital/charge-edit2.html?chapId="+id;
        window.location.href="charge-edit2.html?chapId="+id +
            "&globalPageNum" + globalPageNum + "&globalPageSize" + globalPageSize;
    }
    function deleteCharge(chapId) {
        if(confirm("确定要删除吗?")) {
            //发起异步请求
            $.ajax({
                type: "POST",
                url: origin + "/charge/" + chapId,
                data: {
                    _method:"DELETE",
                    jwtToken:jwtToken
                },
                xhrFields: {
                    withCredentials: true
                },
                crossDomain: true,
                success: function (data) {
                    if(data.code == 200){
                        layer.msg("删除成功！", function(){})
                        queryList(globalPageNum);
                    }else{
                        layer.msg(data.msg, function(){})

                    }
                }
                // TODO: 后端拦截ajax请求进行登陆跳转需要前端相互配合, 每一个ajax请求都需要加上以下的代码
                ,complete : function(xhr, status) {
                    myComplete(xhr, status);
                }
            });
        }
    }

</script>